<?php


namespace addons\official\controller\web;


use addons\official\model\OfficialListForm;
use addons\official\model\OfficialListTable;
use think\Request;
use think\Validate;

class OfficialTable extends OfficialBaseController
{

    protected $model;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        $this->model = new OfficialListTable();
        parent::__construct($request);
    }

    /**
     * 列表
     */
    public function list()
    {
        $data = [];
        $p_id = (int)input('param.pid');
        $son_id = (int)input('param.sid');
        $this->setBasic();
        //查詢父级
        $res = $this->get_cate_pid_in_sid();
        //如果所有子级别和父级一样
        $status = 1;
        if (($p_id == $son_id) && (count($res['son']))) {
            $status = 2;
            $ids = [$p_id];
            foreach ($res['son'] as $item) {
                if (($res['status'] != $item['status']) || ($res['official_rule_id'] != $item['official_rule_id'])) {
                    $status = 1;
                }
                $ids[] = $item['id'];
            }
        }


        if ($son_id) {
            $data = $this->model;
            if ($status == 1) {
                $data = $data->where('official_category_id', $son_id);
            } else {
                $data = $data->whereIn('official_category_id', $ids);
            }
            $data = $data->where('language', $this->language)
                ->where('deletetime', null)
                ->order('weigh', 'desc')
                ->paginate(3, $simple = $this->isMobile(), $config = [
                    'query' => ['pid' => $p_id, 'sid' => $son_id]
                ]);
        }
        return view("web/official_table/list{$this->Mobile}", compact('res', 'data'));
    }

    /**
     * 详情
     */
    public function resume()
    {
        $id = (int)input('param.id');
        if (!$id) {
            $this->error('找不到招聘信息');
        }


        if ($this->request->isPost()) {
            $token = $this->request->post('__token__');
            //验证Token
            if (!$token || !\think\Validate::is($token, "token", ['__token__' => $token])) {
                return json_encode(['code' => 0, 'msg' => 'token失效']);
            }

            $param = input('param.');
            $official_list_table = $this->model->get($param['official_list_table_id']);
            if (empty($official_list_table)) {
                return json_encode(['code' => 0, 'msg' => '该招聘信息查询不到']);
            }

            $rule = [
                "official_list_table_id" => 'require',
                "name"                   => 'require',
                "gender"                 => 'require',
                "birthday"               => 'require',
                "nationality"            => 'require',
                "politics_status"        => 'require',
                "education"              => 'require',
                "native_place"           => 'require',
                "school"                 => 'require',
                "discipline"             => 'require',
                "phone"                  => 'require',
                "email"                  => 'require|email',
                "address"                => 'require',
                "treatment"              => 'require',
                "detail_requirement"     => 'require',
                "captcha"                => 'require|captcha',
            ];
            $msg = [
                "official_list_table_id.require" => __l('table', 'Official_list_table_id') . __l('index', 'No_empty'),
                "name.require"                   => __l('table', 'Name') . __l('index', 'No_empty'),
                "gender.require"                 => __l('table', 'Gender') . __l('index', 'No_empty'),
                "birthday.require"               => __l('table', 'Birthday') . __l('index', 'No_empty'),
                "nationality.require"            => __l('table', 'Nationality') . __l('index', 'No_empty'),
                "politics_status.require"        => __l('table', 'Politics_status') . __l('index', 'No_empty'),
                "education.require"              => __l('table', 'Education') . __l('index', 'No_empty'),
                "native_place.require"           => __l('table', 'Native_place') . __l('index', 'No_empty'),
                "school.require"                 => __l('table', 'School') . __l('index', 'No_empty'),
                "discipline.require"             => __l('table', 'Discipline') . __l('index', 'No_empty'),
                "phone.require"                  => __l('table', 'Phone') . __l('index', 'No_empty'),
                "email.require"                  => __l('table', 'Email') . __l('index', 'No_empty'),
                "email.email"                    => __l('table', 'Incorrect email address'),
                "address.require"                => __l('table', 'Address') . __l('index', 'No_empty'),
                "treatment.require"              => __l('table', 'Treatment') . __l('index', 'No_empty'),
                "detail_requirement.require"     => __l('Table', 'detail_requirement') . __l('index', 'No_empty'),
                "captcha.require"                => __l('index', 'Captcha') . __l('index', 'No_empty'),
                "captcha.captcha"                => __l('index', 'The verification code is incorrect'),
            ];
            $data = [
                "official_list_table_id" => $param['official_list_table_id'],
                "name"                   => $param['name'],
                "gender"                 => $param['gender'],
                "birthday"               => $param['birthday'],
                "nationality"            => $param['nationality'],
                "politics_status"        => $param['politics_status'],
                "education"              => $param['education'],
                "native_place"           => $param['native_place'],
                "school"                 => $param['school'],
                "discipline"             => $param['discipline'],
                "phone"                  => $param['phone'],
                "email"                  => $param['email'],
                "address"                => $param['address'],
                "treatment"              => $param['treatment'],
                "detail_requirement"     => $param['detail_requirement'],
                "captcha"                => $param['captcha'],
            ];
            //验证规则
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                return json_encode(['code' => 0, 'msg' => $validate->getError()]);
            }
            unset($data['captcha']);
            //增加数据
            $data['admin_id'] = $official_list_table['admin_id'];
            $official_list_form = (new OfficialListForm())->create($data);
            if ($official_list_form) {
                return json_encode(['code' => 1, 'msg' => __l('index', 'Submit successfully')]);
            }
        }


        //查询数据
        $table = $this->model
            ->where('id', $id)
            ->where('language', $this->language)
            ->where('deletetime', null)
            ->field(['id,job_title'])
            ->find();
        $this->setBasic(999, $table['job_title']);
        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        return view("web/official_table/resume{$this->Mobile}", compact('table'));
    }

}
